package com.strive.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.strive.common.lock.DistributedLock;
import com.strive.common.model.base.PageResult;
import com.strive.common.model.dto.SysPower;
import com.strive.common.model.dto.SysPowerRole;
import com.strive.common.service.impl.SuperServiceImpl;
import com.strive.user.mapper.SysPowerMapper;
import com.strive.user.mapper.SysPowerRoleMapper;
import com.strive.user.model.SysPowerRolesVo;
import com.strive.user.service.ISysPowerService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description:
 * @author: bingcun.chen
 * @Date: 2022/6/14 11:09
 * @Version 1.0
 */
@Service
public class SysPowerServiceImpl extends SuperServiceImpl<SysPowerMapper, SysPower> implements ISysPowerService {
    private final static String LOCK_KEY_POWER_CODE = "powercode:";

    @Autowired
    private SysPowerRoleMapper sysPowerRoleMapper;

    @Autowired
    private DistributedLock lock;

    @Override
    public boolean insertPowers(List<SysPower> sysPower) {
        return false;
    }

    /**
     * 设置权限
     * @param sysPowerRolesVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean setPowersByRoleId(SysPowerRolesVo sysPowerRolesVo) {
        Set<Long> powers = sysPowerRolesVo.getPowers();
        if(CollectionUtils.isNotEmpty(powers)){
            //根据角色删除对应的权限
            Long id = sysPowerRolesVo.getId();

            LambdaQueryWrapper<SysPowerRole> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysPowerRole::getRoleId, id);
            sysPowerRoleMapper.delete(wrapper);

            //批量插入该角色对应的权限
            sysPowerRoleMapper.batchInsert(id,powers);
        }

        return Boolean.TRUE;
    }

    /**
     * 权限查询
     * @param params
     * @return
     */
    @Override
    public PageResult<SysPower> getPowerDataByMenuId(Map<String, Object> params) {
        Page<SysPower> page = new Page<>(Long.parseLong(MapUtils.getString(params, "pageNum")),Long.parseLong(MapUtils.getString(params, "pageSize")));
        Page<SysPower> sysPowerPage = baseMapper.selectPage(page, new QueryWrapper<SysPower>().eq("menu", params.get("menuId").toString()));
        return PageResult.<SysPower>builder().data(sysPowerPage.getRecords()).status(200).count(sysPowerPage.getTotal()).build();
    }

    /**
     * 添加或者更新权限
     * @param sysPower
     */
    @Override
    public void saveOrUpdateRole(SysPower sysPower) throws Exception {
        if (sysPower.getId() == null) {
            String powerCode = sysPower.getCode();
            super.saveIdempotency(sysPower, lock
                    , LOCK_KEY_POWER_CODE + powerCode, new QueryWrapper<SysPower>().eq("code", powerCode), "权限code已存在");
        } else {
            baseMapper.updateById(sysPower);
        }
    }

    /**
     * 删除权限
     * @param id
     */
    @Override
    public void deletePower(Long id) {
        baseMapper.deleteById(id);
    }
}
